package com.bw.module_service.ui

import android.graphics.Color
import android.widget.TextView
import androidx.activity.viewModels
import android.widget.Toast
import com.alibaba.android.arouter.facade.annotation.Route
import com.bw.lib_common.arouter.Router
import com.bw.lib_common.base.BaseActivity
import com.bw.module_service.R
import com.bw.module_service.adapter.FragmentAdapter
import com.bw.module_service.databinding.ActivitySignBinding
import com.bw.module_service.model.SignStatusTab
import com.bw.module_service.viewmodel.ServiceViewModel
import com.google.android.material.datepicker.MaterialDatePicker
import com.google.android.material.tabs.TabLayoutMediator
import dagger.hilt.android.AndroidEntryPoint
import java.text.SimpleDateFormat
import java.util.Calendar
import java.util.Date
import java.util.Locale
import androidx.core.util.Pair
import com.google.android.material.tabs.TabLayout


@AndroidEntryPoint
@Route(path = Router.Ui.SignActivity)
class SignActivity : BaseActivity<ServiceViewModel, ActivitySignBinding>() {
    override val mLayoutResId: Int
        get() = R.layout.activity_sign
    val startCal: Calendar = Calendar.getInstance()

    private val tabList = listOf(
        SignStatusTab("全部", 0),  // 默认“全部”选中
        SignStatusTab("正常", 1),
        SignStatusTab("迟到", 2),
        SignStatusTab("早退", 3),
        SignStatusTab("缺勤", 4)
    )


    override fun initView() {
        super.initView()
    }


    override fun initData() {
        mBinding.iconBack.setOnClickListener { finish() }
        mBinding.selectData.setOnClickListener {
            showMaterialDateRangePicker()
        }

    }
// 添加依赖 (build.gradle)
// implementation 'com.google.android.material:material:1.6.0'

    fun showMaterialDateRangePicker() {
        val today = MaterialDatePicker.todayInUtcMilliseconds()
        val calendar = Calendar.getInstance()
        calendar.add(Calendar.MONTH, -1)
        val lastMonth = calendar.timeInMillis

        // 创建日期范围选择器
        val dateRangePicker = MaterialDatePicker.Builder.dateRangePicker()
            .setTheme(com.bw.lib_common.R.style.MyMaterialCalendarTheme)
            .setTitleText("选择日期范围")
           .setSelection(Pair(lastMonth, today))
            .build()

        // 显示选择器
        dateRangePicker.show(supportFragmentManager, "DATE_RANGE_PICKER")

        // 处理选择结果
        dateRangePicker.addOnPositiveButtonClickListener { dateRange ->
            val startDate = Date(dateRange.first ?: 0)
            val endDate = Date(dateRange.second ?: 0)

            // 格式化日期显示
            val sdf = SimpleDateFormat("yyyy-MM-dd", Locale.getDefault())
            val startDateStr = sdf.format(startDate)
            val endDateStr = sdf.format(endDate)
            mBinding.etData.setText("$startDateStr 至 $endDateStr")
        }
    }
    override fun initListener() {


        val adapter = FragmentAdapter(this, tabList.size)
        mBinding.viewPager.adapter = adapter

//        TabLayoutMediator(mBinding.tabLayout, mBinding.viewPager) { tab, position ->
//            tab.text = tabList[position].text
//        }.attach()
        TabLayoutMediator(mBinding.tabLayout, mBinding.viewPager) { tab, position ->
            val view = layoutInflater.inflate(R.layout.item_sign_status_tab, null)
            val textView = view.findViewById<TextView>(R.id.tv1)
            textView.text = tabList[position].text
            tab.customView = view

            // 添加 Tab 选中状态监听器
            tab.view?.setOnClickListener {
                updateTabStyles(tabLayout = mBinding.tabLayout, selectedPosition = position)
            }
        }.attach()

        // 初始设置第一个为选中样式
        updateTabStyles(tabLayout = mBinding.tabLayout, selectedPosition = 0)
    }
    // 更新所有 Tab 样式的方法
    private fun updateTabStyles(tabLayout: TabLayout, selectedPosition: Int) {
        for (i in 0 until tabLayout.tabCount) {
            val tab = tabLayout.getTabAt(i)
            val view = tab?.customView
            val textView = view?.findViewById<TextView>(R.id.tv1)

            if (i == selectedPosition) {
                textView?.setTextColor(Color.WHITE)
                textView?.setBackgroundResource(R.drawable.shape_blue)
            } else {
                textView?.setTextColor(Color.BLACK)
                textView?.setBackgroundResource(R.drawable.dl_ba1) // 原始默认背景
            }
        }
    }



}